<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="propellingForm" label-position="left" label-width="140px">

      <el-form-item label="位置" prop="locationID">
        <el-select v-model="propellingForm.locationID" placeholder="请选择">
          <el-option
            v-for="item in locationOptions"
            :key="item.ID"
            :label="item.title"
            :value="item.ID"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="资源" prop="blackboardID">
        <el-select v-model="propellingForm.blackboardID" placeholder="请选择">
          <el-option
            v-for="item in blackboardOptions"
            :key="item.ID"
            :label="item.title"
            :value="item.ID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="有效期">
        <el-date-picker 
          v-model="propellingForm.begin_time"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始日期"
          align="right"
        />
        <el-date-picker 
          v-model="propellingForm.end_time"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始日期"
          align="right"
        />
      </el-form-item>

      <el-form-item label="会员等级" prop="gradeID">
        <el-checkbox-group v-model="propellingForm.gradeID">
          <el-checkbox v-for="grade in gradeOptions" :key="grade.ID" :label="grade.ID">{{ grade.title }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="投放区域" prop="districtID">
        <el-checkbox-group v-model="propellingForm.districtID">
          <el-checkbox v-for="area in districtOptions" :key="area.ID" :label="area.ID">{{ area.title }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="propellingForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="propellingForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="propellingForm.ID > 0 && propellingForm.status.value < 6" type="danger" @click="handleDelete(propellingForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { savePropelling, removePropellingByID, getPropelling } from '@/api/popularize/propelling'
import { getLocationAvabile } from '@/api/popularize/location'
import { getBlackboardAvabile } from '@/api/popularize/blackboard'
import { getGradeAvabile } from '@/api/customer/grade'
import { getDistrictAvabile } from '@/api/foundation/district'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      locationOptions: [],
      blackboardOptions: [],
      districtOptions: [
      ],
      gradeOptions: [
      ],
      propellingForm: {
        locationID: undefined,
        districtID: [],
        begin_time: undefined,
        end_time: undefined,
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      rules: {

      }
    }
  },
  created() {
    let propellingID = this.$route.query && this.$route.query.propellingID
    if (propellingID !== undefined) {
      propellingID = parseInt(propellingID)
      if (!isNaN(propellingID)) {
        this.listLoading = true
        const formData = {
          'propellingID': propellingID
        }
        getPropelling(formData).then(response => {
          if (response.status === 200) {
            this.propellingForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(propellingID)
  },
  mounted() {
    this.getLoationList()
    this.getBlackboardList()
    this.getDistrictList()
    this.getGradeList()
  },
  methods: {
    getDistrictList() {
      const _this = this
      getDistrictAvabile().then(response => {
        if (response.status === 200) {
          _this.districtOptions = response.data.list
          console.log(_this.districtOptions)
        }
      })
    },
    getGradeList() {
      const _this = this
      getGradeAvabile().then(response => {
        if (response.status === 200) {
          _this.gradeOptions = response.data.list
          console.log(_this.gradeOptions)
        }
      })
    },
    getLoationList() {
      getLocationAvabile().then(response => {
        if (response.status === 200) {
          this.locationOptions = response.data.list
        }
      })
    },
    getBlackboardList() {
      getBlackboardAvabile().then(response => {
        if (response.status === 200) {
          this.blackboardOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'propelling' })
    },
    createData() {
      this.listLoading = true

      const _this = this
      let formData = {
      }
      formData = Object.assign(formData, _this.propellingForm)
      formData.status = 6

      if (formData.ID > 0) {
        formData.propellingID = formData.ID
      }
      savePropelling(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'propelling' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    draftData() {
      this.listLoading = true

      const _this = this
      let formData = {
      }
      formData = Object.assign(formData, _this.propellingForm)
      formData.status = 1

      if (formData.ID > 0) {
        formData.propellingID = formData.ID
      }
      savePropelling(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'propelling' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.listLoading = true

      removePropellingByID(this.listQuery).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'propelling' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
